﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VedioOnline.Tools.Dao;
using VideoOnline.FFmpeg;
using VedioOnline.Tools;


namespace VideoOnline.Controllers
{
    public class HomeController : Controller
    {
        BaseDao dao = new BaseDao();
        public ActionResult Index()
        {
            //Main main = new Main();
            //main.Test();

            //GlobalClass.Add();
            //ViewBag.A = GlobalClass.n;

            return View("Index");
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your application description page.";
            
            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        public ActionResult testbtn()
        {
            string dir = Server.MapPath("~/Upload/Source/S.H.E.belief.mp4");
            string name = "S.H.E.belief.mp4";
            Dictionary<string, object> map = new Dictionary<string, object>()
            {
                ["FileDir"]=dir,
                ["VideoName"]=name
            };
            GlobalClass.TransList.Add(map);
            return Json("0");
        }

        public ActionResult VedioUpLoad()
        {
            return View("VedioUpLoad");
        }


        public ActionResult API_VedioUpLoad()
        {
            var sr = new StreamReader(Request.InputStream);
            var stream = sr.ReadToEnd();
            JObject obj = JObject.Parse(stream);

            string Title = obj["Title"] == null ? null : obj["Title"].ToString();
            string VideoUrl = obj["VideoUrl"] == null ? null : obj["VideoUrl"].ToString();
            string ImageUrl = obj["ImageUrl"] == null ? null : obj["ImageUrl"].ToString();
            string VttUrl = obj["VttUrl"] == null ? null : obj["VttUrl"].ToString();


            Dictionary<string, object> result = new Dictionary<string, object>();
            if (Title == null || VideoUrl == null || ImageUrl == null || VttUrl == null )
            {
                result.Add("result", "error");
                return Json(result);
            }

            try
            {
                string CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                Dictionary<string, object> map = new Dictionary<string, object>()
                {
                    ["Title"] = Title,
                    ["VideoUrl"] = VideoUrl,
                    ["ImageUrl"] = ImageUrl,
                    ["VttUrl"] = VttUrl,
                    ["UploadTime"] = DateTime.Now.ToString(),
                    ["HasTransform"] = 0,
                    ["State"]=0,


                };
                dao.save("t_d_UpLoadVideo", map);

            }
            catch (Exception e)
            {
                Console.Write(e.Message.ToString());
                result.Add("result", "error:" + e.Message.ToString().Replace("'", ""));
                return Json(result);

            }
            result.Add("result", "OK");
            return Json(result);
        }


        public ActionResult PiracyUpLoadView()
        {
            return View("PiracyUpLoad");
        }

        public ActionResult API_PiracyUpLoad()
        {
            var sr = new StreamReader(Request.InputStream);
            var stream = sr.ReadToEnd();
            JObject obj = JObject.Parse(stream);

            string Title = obj["Title"] == null ? null : obj["Title"].ToString();
            string M3U8Path = obj["M3U8Path"] == null ? null : obj["M3U8Path"].ToString();
            string ImageUrl = obj["ImageUrl"] == null ? null : obj["ImageUrl"].ToString();
            string VttUrl = obj["VttUrl"] == null ? null : obj["VttUrl"].ToString();


            Dictionary<string, object> result = new Dictionary<string, object>();
            if (Title == null || M3U8Path == null || ImageUrl == null || VttUrl == null)
            {
                result.Add("result", "error");
                return Json(result);
            }

            try
            {
                string CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                Dictionary<string, object> map = new Dictionary<string, object>()
                {
                    ["Title"] = Title,
                    ["M3U8Path"] = M3U8Path,
                    ["ImageUrl"] = ImageUrl,
                    ["VttUrl"] = VttUrl,
                    ["UploadTime"] = DateTime.Now.ToString(),
                    ["HasTransform"] = 0,
                    ["State"] = 2,


                };
                dao.save("t_d_UpLoadVideo", map);

            }
            catch (Exception e)
            {
                Console.Write(e.Message.ToString());
                result.Add("result", "error:" + e.Message.ToString().Replace("'", ""));
                return Json(result);

            }
            result.Add("result", "OK");
            return Json(result);
        }

        /// <summary>
        /// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        ///  视频列表部分
        /// </summary>
        /// <returns></returns>
        public ActionResult VideoListView()
        {
            return View("VideoList");
        }

        public ActionResult UploadTableRequest()
        {
            int page = Int32.Parse(Request.Form["page"]);
            int limit = Int32.Parse(Request.Form["limit"]);
            string condition = Request.Form["condition"].ToString();
            string SortName = Request.Form["SortName"].ToString();
            string Order = Request.Form["Order"].ToString();


            try
            {
                //以下是分页查询语句
                string sql = "", SqlNum = "";
                sql = " select top " + limit.ToString() + " * from " +
                        " ( " +
                        " SELECT ROW_NUMBER() OVER (order by " + SortName + " " + Order + ") as rowNumber,VideoId,VideoUrl,Title,ImageUrl,VttUrl," +
                        " CONVERT(VARCHAR(20),UploadTime,120) as UploadTime,HasTransform,State,OutputPath,m3u8Path " +
                        " FROM t_d_UpLoadVideo " +
                        " where 1=1 " + condition +
                        " ) as s " +
                        " where rowNumber>" + (limit * (page - 1)).ToString();

                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                list = dao.GetList(sql, new string[] { "VideoId", "VideoUrl", "Title", "ImageUrl", "VttUrl", "UploadTime", "HasTransform", "State", "OutputPath","m3u8Path" });


                SqlNum = "select count(*) as TotalNum FROM t_d_UpLoadVideo where 1=1 " + condition;
                List<Dictionary<string, object>> ListNum = new List<Dictionary<string, object>>();
                ListNum = dao.GetList(SqlNum, new string[] { "TotalNum" });

                TableData td = new TableData();
                td.code = "";
                td.msg = "";
                td.count = int.Parse(ListNum[0]["TotalNum"].ToString());
                td.data = list;
                return Json(td);
            }
            catch (Exception ex)
            {
                Dictionary<string, object> datalog = new Dictionary<string, object>();
                datalog.Add("type", int.Parse("1"));
                datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                datalog.Add("OpID", int.Parse("1"));
                datalog.Add("OpLog", "异常：加载需求管理表格异常 /BCSSController/SSTableRequest:" + ex.Message.ToString().Replace("'", ""));
                dao.save("T_LOG", datalog);
                return Json('0');
            }
        }

        [HttpPost]
        public ActionResult API_TransfromVideo()
        {
            string VideoUrl = Request.Form["VideoUrl"] == null ? null : Request.Form["VideoUrl"].ToString();
            string VideoId = Request.Form["VideoId"] == null ? null : Request.Form["VideoId"].ToString();

            string dir = Server.MapPath(VideoUrl);
            string Videoname = dir.Substring(dir.LastIndexOf("\\") + 1, dir.LastIndexOf(".") - dir.LastIndexOf("\\") - 1);

            Dictionary<string, object> map = new Dictionary<string, object>()
            {
                ["FileDir"] = dir,
                ["VideoName"] = Videoname,
                ["VideoId"] = VideoId,
            };
            GlobalClass.TransList.Add(map);
            string sql = "update t_d_UpLoadVideo set State=1 where VideoId=" + VideoId;
            BaseDao.execute(sql);
            Dictionary<string, object> result = new Dictionary<string, object>();


            result.Add("result", "OK");
            return Json(result);
        }

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///主题

        public ActionResult SubjectNewView()
        {
            return View("SubjectNew");
        }

        /// <summary>
        /// 接收新增主题
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult API_SubjectNew()
        {
            var sr = new StreamReader(Request.InputStream);
            var stream = sr.ReadToEnd();
            JObject obj = JObject.Parse(stream);

            string Title = obj["Title"] == null ? null : obj["Title"].ToString();
            string Content = obj["Content"] == null ? null : obj["Content"].ToString();
            string ImageUrl = obj["ImageUrl"] == null ? null : obj["ImageUrl"].ToString();



            Dictionary<string, object> result = new Dictionary<string, object>();
            if (Title == null || Content == null || ImageUrl ==null)
            {
                result.Add("result", "error");
                return Json(result);
            }

            try
            {
                string CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                Dictionary<string, object> map = new Dictionary<string, object>()
                {
                    ["Title"] = Title,
                    ["Content"] = Content,
                    ["IsDelete"] = 0,
                    ["ImageUrl"] = ImageUrl,
                    ["UpdateTime"] = DateTime.Now.ToString(),
                };
                dao.save("t_o_Subject", map);

            }
            catch (Exception e)
            {
                Console.Write(e.Message.ToString());
                result.Add("result", "error:" + e.Message.ToString().Replace("'", ""));
                return Json(result);

            }
            result.Add("result", "OK");
            return Json(result);
        }


        public ActionResult SubjectTableRequest()
        {
            int page = Int32.Parse(Request.Form["page"]);
            int limit = Int32.Parse(Request.Form["limit"]);
            string condition = Request.Form["condition"].ToString();
            string SortName = Request.Form["SortName"].ToString();
            string Order = Request.Form["Order"].ToString();


            try
            {
                //以下是分页查询语句
                string sql = "", SqlNum = "";
                sql = " select top " + limit.ToString() + " * from " +
                        " ( " +
                        " SELECT ROW_NUMBER() OVER (order by " + SortName + " " + Order + ") as rowNumber,SubjectId,Content,IsDelete,Title," +
                        " CONVERT(VARCHAR(20),UpdateTime,120) as UpdateTime" +
                        " FROM t_o_Subject " +
                        " where 1=1 " + condition +
                        " ) as s " +
                        " where rowNumber>" + (limit * (page - 1)).ToString();

                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                list = dao.GetList(sql, new string[] { "SubjectId", "Content", "IsDelete", "Title" });


                SqlNum = "select count(*) as TotalNum FROM t_o_Subject where 1=1 " + condition;
                List<Dictionary<string, object>> ListNum = new List<Dictionary<string, object>>();
                ListNum = dao.GetList(SqlNum, new string[] { "TotalNum" });

                TableData td = new TableData();
                td.code = "";
                td.msg = "";
                td.count = int.Parse(ListNum[0]["TotalNum"].ToString());
                td.data = list;
                return Json(td);
            }
            catch (Exception ex)
            {
                Dictionary<string, object> datalog = new Dictionary<string, object>();
                datalog.Add("type", int.Parse("1"));
                datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                datalog.Add("OpID", int.Parse("1"));
                datalog.Add("OpLog", "异常：加载需求管理表格异常 /Home/SubjectTableRequest:" + ex.Message.ToString().Replace("'", ""));
                dao.save("T_LOG", datalog);
                return Json('0');
            }
        }


        public ActionResult SubjectView()
        {
            return View("Subject");
        }


        public ActionResult SubjectEditView()
        {
            string SubjectId = Request.QueryString["SubjectId"] == null ? null : Request.QueryString["SubjectId"].ToString();

            try
            {
                string sql = "select SubjectId,Content,ImageUrl,Title,CONVERT(VARCHAR(20),UpdateTime,120) as UpdateTime from t_o_Subject where IsDelete<>1 and SubjectId=" + SubjectId;
                var map = dao.GetList(sql, new string[] { "ImageUrl", "SubjectId", "Content", "Title", "UpdateTime" })[0];
                ViewBag.Map = map;
                return View("SubjectEdit");

            }
            catch (Exception e)
            {

                Console.Write(e.Message.ToString());
                return Content(e.Message.ToString().Replace("'", ""));
            }

        }


        [HttpPost]
        [ValidateInput(false)]
        public ActionResult API_SubjectEdit()
        {
            var sr = new StreamReader(Request.InputStream);
            var stream = sr.ReadToEnd();
            JObject obj = JObject.Parse(stream);

            string Title = obj["Title"] == null ? null : obj["Title"].ToString();
            string Content = obj["Content"] == null ? null : obj["Content"].ToString();
            string SubjectId = obj["SubjectId"] == null ? null : obj["SubjectId"].ToString();
            string ImageUrl = obj["ImageUrl"] == null ? null : obj["ImageUrl"].ToString();

            Dictionary<string, object> result = new Dictionary<string, object>();
            if (Title == null || Content == null)
            {
                result.Add("result", "error");
                return Json(result);
            }

            try
            {
                string CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                Dictionary<string, object> map = new Dictionary<string, object>()
                {
                    ["Title"] = Title,
                    ["Content"] = Content,
                    ["IsDelete"] = 0,
                    ["ImageUrl"] = ImageUrl,
                    ["UpdateTime"] = DateTime.Now.ToString(),
                };
                dao.update("t_o_Subject", map,"SubjectId="+SubjectId);

            }
            catch (Exception e)
            {
                Console.Write(e.Message.ToString());
                result.Add("result", "error:" + e.Message.ToString().Replace("'", ""));
                return Json(result);

            }
            result.Add("result", "OK");
            return Json(result);
        }


/////////////////////////////////////////////////////
///视频绑定
        public ActionResult SubjectVideoView()
        {
            string SubjectId = Request.QueryString["SubjectId"] == null ? null : Request.QueryString["SubjectId"].ToString();
            string sql = "select VideoId,Title from t_d_UpLoadVideo order by VideoId Desc";
            var VideoList = dao.GetList(sql, new string[] { "VideoId", "Title" });
            ViewBag.VideoList = VideoList;
            ViewBag.SubjectId = SubjectId;
            return View("SubjectVideo");
        }


        public ActionResult SVTableRequest()
        {
            int page = Int32.Parse(Request.Form["page"]);
            int limit = Int32.Parse(Request.Form["limit"]);
            string condition = Request.Form["condition"].ToString();
            string SortName = Request.Form["SortName"].ToString();
            string Order = Request.Form["Order"].ToString();


            try
            {
                //以下是分页查询语句
                string sql = "", SqlNum = "";
                sql = " select top " + limit.ToString() + " * from " +
                        " ( " +
                        " SELECT ROW_NUMBER() OVER (order by " + SortName + " " + Order + ") as rowNumber,SVId,Title,SubjectId,SortNum" +
                        " " +
                        " FROM v_SubVid " +
                        " where 1=1 " + condition +
                        " ) as s " +
                        " where rowNumber>" + (limit * (page - 1)).ToString();

                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                list = dao.GetList(sql, new string[] { "SVId", "Title","SubjectId","SortNum" });


                SqlNum = "select count(*) as TotalNum FROM v_SubVid where 1=1 " + condition;
                List<Dictionary<string, object>> ListNum = new List<Dictionary<string, object>>();
                ListNum = dao.GetList(SqlNum, new string[] { "TotalNum" });

                TableData td = new TableData();
                td.code = "";
                td.msg = "";
                td.count = int.Parse(ListNum[0]["TotalNum"].ToString());
                td.data = list;
                return Json(td);
            }
            catch (Exception ex)
            {
                Dictionary<string, object> datalog = new Dictionary<string, object>();
                datalog.Add("type", int.Parse("1"));
                datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                datalog.Add("OpID", int.Parse("1"));
                datalog.Add("OpLog", "异常：加载需求管理表格异常 /Home/SubjectTableRequest:" + ex.Message.ToString().Replace("'", ""));
                dao.save("T_LOG", datalog);
                return Json('0');
            }
        }


        public ActionResult API_SubjectVideoAdd()
        {
            var sr = new StreamReader(Request.InputStream);
            var stream = sr.ReadToEnd();
            JObject obj = JObject.Parse(stream);

            string VideoId = obj["VideoId"] == null ? null : obj["VideoId"].ToString();
            string SortNum = obj["SortNum"] == null ? null : obj["SortNum"].ToString();
            string SubjectId = obj["SubjectId"] == null ? null : obj["SubjectId"].ToString();



            Dictionary<string, object> result = new Dictionary<string, object>();


            try
            {
                string CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                Dictionary<string, object> map = new Dictionary<string, object>()
                {
                    ["VideoId"] = VideoId,
                    ["SortNum"] = SortNum,
                    ["SubjectId"] = SubjectId,
                };
                dao.save("t_o_SubVid", map);

            }
            catch (Exception e)
            {
                Console.Write(e.Message.ToString());
                result.Add("result", "error:" + e.Message.ToString().Replace("'", ""));
                return Json(result);

            }
            result.Add("result", "OK");
            return Json(result);
        }

        public ActionResult API_SubjectVideoDel()
        {
            string SVId = Request.Form["SVId"].ToString();
            string sql = "delete t_o_SubVid where SVId=" + SVId;

            Dictionary<string, object> result = new Dictionary<string, object>();

            try
            {
                BaseDao.execute(sql);

            }
            catch (Exception e)
            {
                Console.Write(e.Message.ToString());
                result.Add("result", "error:" + e.Message.ToString().Replace("'", ""));
                return Json(result);

            }
            result.Add("result", "OK");
            return Json(result);
            


        }



        public ActionResult TypeDYView()
        {
            
            string sql = "select SubjectId,Title from t_o_Subject";
            var SubjectList = dao.GetList(sql, new string[] { "SubjectId", "Title" });
            ViewBag.SubjectList = SubjectList;

            return View("TypeDY");
        }


        public ActionResult API_TypeDYAdd()
        {
            string SubjectId = Request.Form["SubjectId"].ToString();

            Dictionary<string, object> result = new Dictionary<string, object>();

            try
            {
                Dictionary<string, object> map = new Dictionary<string, object>()
                {
                    ["SubjectId"] = SubjectId,

                };
                dao.save("t_o_TypeDY", map);
            }
            catch (Exception e)
            {
                Console.Write(e.Message.ToString());
                result.Add("result", "error:" + e.Message.ToString().Replace("'", ""));
                return Json(result);

            }
            result.Add("result", "OK");
            return Json(result);
        }

        public ActionResult API_TypeDYDel()
        {
            string TypeDYId = Request.Form["TypeDYId"].ToString();
            string sql = "delete t_o_TypeDY where TypeDYId=" + TypeDYId;

            Dictionary<string, object> result = new Dictionary<string, object>();

            try
            {
                BaseDao.execute(sql);

            }
            catch (Exception e)
            {
                Console.Write(e.Message.ToString());
                result.Add("result", "error:" + e.Message.ToString().Replace("'", ""));
                return Json(result);

            }
            result.Add("result", "OK");
            return Json(result);



        }

        public ActionResult TypeDYTableRequest()
        {
            int page = Int32.Parse(Request.Form["page"]);
            int limit = Int32.Parse(Request.Form["limit"]);
            string condition = Request.Form["condition"].ToString();
            string SortName = Request.Form["SortName"].ToString();
            string Order = Request.Form["Order"].ToString();


            try
            {
                //以下是分页查询语句
                string sql = "", SqlNum = "";
                sql = " select top " + limit.ToString() + " * from " +
                        " ( " +
                        " SELECT ROW_NUMBER() OVER (order by " + SortName + " " + Order + ") as rowNumber,TypeDYId,Title,SubjectId" +
                        " " +
                        " FROM v_TypeDY " +
                        " where 1=1 " + condition +
                        " ) as s " +
                        " where rowNumber>" + (limit * (page - 1)).ToString();

                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                list = dao.GetList(sql, new string[] { "TypeDYId", "Title", "SubjectId"});


                SqlNum = "select count(*) as TotalNum FROM v_TypeDY where 1=1 " + condition;
                List<Dictionary<string, object>> ListNum = new List<Dictionary<string, object>>();
                ListNum = dao.GetList(SqlNum, new string[] { "TotalNum" });

                TableData td = new TableData();
                td.code = "";
                td.msg = "";
                td.count = int.Parse(ListNum[0]["TotalNum"].ToString());
                td.data = list;
                return Json(td);
            }
            catch (Exception ex)
            {
                Dictionary<string, object> datalog = new Dictionary<string, object>();
                datalog.Add("type", int.Parse("1"));
                datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                datalog.Add("OpID", int.Parse("1"));
                datalog.Add("OpLog", "异常：加载需求管理表格异常 /Home/SubjectTableRequest:" + ex.Message.ToString().Replace("'", ""));
                dao.save("T_LOG", datalog);
                return Json('0');
            }
        }



        ////////////////////////////////////////////////////////////////////////////////
        ///剧集类别


        public ActionResult TypeJJView()
        {

            string sql = "select SubjectId,Title from t_o_Subject";
            var SubjectList = dao.GetList(sql, new string[] { "SubjectId", "Title" });
            ViewBag.SubjectList = SubjectList;

            return View("TypeJJ");
        }


        public ActionResult API_TypeJJAdd()
        {
            string SubjectId = Request.Form["SubjectId"].ToString();

            Dictionary<string, object> result = new Dictionary<string, object>();

            try
            {
                Dictionary<string, object> map = new Dictionary<string, object>()
                {
                    ["SubjectId"] = SubjectId,
                };
                dao.save("t_o_TypeJJ", map);
            }
            catch (Exception e)
            {
                Console.Write(e.Message.ToString());
                result.Add("result", "error:" + e.Message.ToString().Replace("'", ""));
                return Json(result);

            }
            result.Add("result", "OK");
            return Json(result);
        }

        public ActionResult API_TypeJJDel()
        {
            string TypeJJId = Request.Form["TypeJJId"].ToString();
            string sql = "delete t_o_TypeJJ where TypeJJId=" + TypeJJId;

            Dictionary<string, object> result = new Dictionary<string, object>();

            try
            {
                BaseDao.execute(sql);

            }
            catch (Exception e)
            {
                Console.Write(e.Message.ToString());
                result.Add("result", "error:" + e.Message.ToString().Replace("'", ""));
                return Json(result);

            }
            result.Add("result", "OK");
            return Json(result);



        }

        public ActionResult TypeJJTableRequest()
        {
            int page = Int32.Parse(Request.Form["page"]);
            int limit = Int32.Parse(Request.Form["limit"]);
            string condition = Request.Form["condition"].ToString();
            string SortName = Request.Form["SortName"].ToString();
            string Order = Request.Form["Order"].ToString();


            try
            {
                //以下是分页查询语句
                string sql = "", SqlNum = "";
                sql = " select top " + limit.ToString() + " * from " +
                        " ( " +
                        " SELECT ROW_NUMBER() OVER (order by " + SortName + " " + Order + ") as rowNumber,TypeJJId,Title,SubjectId" +
                        " " +
                        " FROM v_TypeJJ " +
                        " where 1=1 " + condition +
                        " ) as s " +
                        " where rowNumber>" + (limit * (page - 1)).ToString();

                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                list = dao.GetList(sql, new string[] { "TypeJJId", "Title", "SubjectId" });


                SqlNum = "select count(*) as TotalNum FROM v_TypeJJ where 1=1 " + condition;
                List<Dictionary<string, object>> ListNum = new List<Dictionary<string, object>>();
                ListNum = dao.GetList(SqlNum, new string[] { "TotalNum" });

                TableData td = new TableData();
                td.code = "";
                td.msg = "";
                td.count = int.Parse(ListNum[0]["TotalNum"].ToString());
                td.data = list;
                return Json(td);
            }
            catch (Exception ex)
            {
                Dictionary<string, object> datalog = new Dictionary<string, object>();
                datalog.Add("type", int.Parse("1"));
                datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                datalog.Add("OpID", int.Parse("1"));
                datalog.Add("OpLog", "异常：加载需求管理表格异常 /Home/SubjectTableRequest:" + ex.Message.ToString().Replace("'", ""));
                dao.save("T_LOG", datalog);
                return Json('0');
            }
        }


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///综艺分类

        public ActionResult TypeZYView()
        {

            string sql = "select SubjectId,Title from t_o_Subject";
            var SubjectList = dao.GetList(sql, new string[] { "SubjectId", "Title" });
            ViewBag.SubjectList = SubjectList;

            return View("TypeZY");
        }


        public ActionResult API_TypeZYAdd()
        {
            string SubjectId = Request.Form["SubjectId"].ToString();

            Dictionary<string, object> result = new Dictionary<string, object>();

            try
            {
                Dictionary<string, object> map = new Dictionary<string, object>()
                {
                    ["SubjectId"] = SubjectId,
                };
                dao.save("t_o_TypeZY", map);
            }
            catch (Exception e)
            {
                Console.Write(e.Message.ToString());
                result.Add("result", "error:" + e.Message.ToString().Replace("'", ""));
                return Json(result);

            }
            result.Add("result", "OK");
            return Json(result);
        }

        public ActionResult API_TypeZYDel()
        {
            string TypeZYId = Request.Form["TypeZYId"].ToString();
            string sql = "delete t_o_TypeZY where TypeZYId=" + TypeZYId;

            Dictionary<string, object> result = new Dictionary<string, object>();

            try
            {
                BaseDao.execute(sql);

            }
            catch (Exception e)
            {
                Console.Write(e.Message.ToString());
                result.Add("result", "error:" + e.Message.ToString().Replace("'", ""));
                return Json(result);

            }
            result.Add("result", "OK");
            return Json(result);



        }

        public ActionResult TypeZYTableRequest()
        {
            int page = Int32.Parse(Request.Form["page"]);
            int limit = Int32.Parse(Request.Form["limit"]);
            string condition = Request.Form["condition"].ToString();
            string SortName = Request.Form["SortName"].ToString();
            string Order = Request.Form["Order"].ToString();


            try
            {
                //以下是分页查询语句
                string sql = "", SqlNum = "";
                sql = " select top " + limit.ToString() + " * from " +
                        " ( " +
                        " SELECT ROW_NUMBER() OVER (order by " + SortName + " " + Order + ") as rowNumber,TypeZYId,Title,SubjectId" +
                        " " +
                        " FROM v_TypeZY " +
                        " where 1=1 " + condition +
                        " ) as s " +
                        " where rowNumber>" + (limit * (page - 1)).ToString();

                List<Dictionary<string, object>> list = new List<Dictionary<string, object>>();
                list = dao.GetList(sql, new string[] { "TypeZYId", "Title", "SubjectId" });


                SqlNum = "select count(*) as TotalNum FROM v_TypeZY where 1=1 " + condition;
                List<Dictionary<string, object>> ListNum = new List<Dictionary<string, object>>();
                ListNum = dao.GetList(SqlNum, new string[] { "TotalNum" });

                TableData td = new TableData();
                td.code = "";
                td.msg = "";
                td.count = int.Parse(ListNum[0]["TotalNum"].ToString());
                td.data = list;
                return Json(td);
            }
            catch (Exception ex)
            {
                Dictionary<string, object> datalog = new Dictionary<string, object>();
                datalog.Add("type", int.Parse("1"));
                datalog.Add("OpTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                datalog.Add("OpID", int.Parse("1"));
                datalog.Add("OpLog", "异常：加载需求管理表格异常 /Home/SubjectTableRequest:" + ex.Message.ToString().Replace("'", ""));
                dao.save("T_LOG", datalog);
                return Json('0');
            }
        }    



    }
}